﻿<Window x:Class="WpfPitchTuner.View.Preferences"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:WpfPitchTuner.ViewModel"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:provider="clr-namespace:WpfPitchTuner.Provider"
        xmlns:helper="clr-namespace:WpfPitchTuner.Helper"
        xmlns:view="clr-namespace:WpfPitchTuner.View"
        xmlns:business="clr-namespace:WpfPitchTuner.Business"
        xmlns:settings="clr-namespace:WpfPitchTuner.Settings"
        ResizeMode="NoResize"
        WindowStyle="ToolWindow"
        Title="Preferences" 
        Icon="/img/settings_ico.png"
        WindowStartupLocation="CenterOwner"        
        Height="430" 
        Width="300"        
        >

    <Window.DataContext>
        <Binding Source="{x:Static provider:ViewModelLocator.Instance}" Path="PreferencesVM" />
    </Window.DataContext>

    <Window.Resources>
        
        <ObjectDataProvider x:Key="version"
            ObjectType="{x:Type business:Utility}"
            MethodName="GetProductVersion" />
    </Window.Resources>

    <StackPanel Margin="10">
        <TabControl>
            <TabItem Header="Layout">
                <StackPanel helper:NotifierPanelHelper.EnableNotifier="True">
                    <TextBlock HorizontalAlignment="Center" Margin="0, 10, 0, 10">Skin</TextBlock>
                    <ComboBox Margin="10, 0, 10, 0" ItemsSource="{Binding Source={StaticResource skins}}" SelectedItem="{Binding Source={x:Static provider:ViewModelLocator.Instance}, Path=MainVM.SelectedSkin}" />
                </StackPanel>
            </TabItem>
            <TabItem Header="Input">
                <StackPanel helper:NotifierPanelHelper.EnableNotifier="True">
                    <TextBlock HorizontalAlignment="Center" Margin="0, 10, 0, 10">Device</TextBlock>
                    <ComboBox Margin="10, 0, 10, 0" ItemsSource="{Binding Devices}" SelectedIndex="{Binding SelectedDevice}" />
                    <TextBlock TextAlignment="Center" Margin="0, 10, 0, 10">Threshold</TextBlock>
                    <Button Margin="0, 0, 0, 10" Content="Reset" Width="70" Command="{Binding Source={x:Static provider:ViewModelLocator.Instance}, Path=MainVM.ResetThresholdCommand}" />
                </StackPanel>
            </TabItem>
            <TabItem Header="Network">
                <StackPanel HorizontalAlignment="Center" Margin="0, 10, 0 , 0" helper:NotifierPanelHelper.EnableNotifier="True">
                    <Button Margin="5" Content="Check" Command="{Binding CheckForUpdatesCommand}" />
                    <CheckBox x:Name="chk_useproxy" HorizontalAlignment="Center" Content="Use proxy" Margin="0, 5, 0, 0" IsChecked="{Binding EnableProxy}" />
                    <GroupBox Header="Proxy" Margin="0, 5, 0, 0" IsEnabled="{Binding ElementName=chk_useproxy, Path=IsChecked}">
                        <StackPanel helper:NotifierPanelHelper.EnableNotifier="True">
                            <TextBlock HorizontalAlignment="Center">Address</TextBlock>
                            <TextBox Width="200" Text="{Binding Host, Mode=TwoWay}"></TextBox>
                            <TextBlock HorizontalAlignment="Center">Port</TextBlock>
                            <TextBox HorizontalContentAlignment="Right" Width="70" Text="{Binding Port, Mode=TwoWay}"></TextBox>
                        </StackPanel>
                    </GroupBox>
                    <GroupBox Header="Credentials" Margin="0, 5, 0, 0" IsEnabled="{Binding ElementName=chk_useproxy, Path=IsChecked}">
                        <StackPanel helper:NotifierPanelHelper.EnableNotifier="True">
                            <TextBlock HorizontalAlignment="Center">User</TextBlock>
                            <TextBox Width="150" Text="{Binding ProxyUser, Mode=TwoWay}"></TextBox>
                            <TextBlock HorizontalAlignment="Center">Password</TextBlock>
                            <PasswordBox Width="150" 
                                         helper:PasswordHelper.Attach="True" 
                                         helper:PasswordHelper.Password="{Binding Path=ProxyPassword, Mode=TwoWay}"
                                         ></PasswordBox>
                            <TextBlock HorizontalAlignment="Center">Domain</TextBlock>
                            <TextBox Width="150" Text="{Binding ProxyDomain, Mode=TwoWay}"></TextBox>
                        </StackPanel>
                    </GroupBox>                    
                </StackPanel>
            </TabItem>
            <TabItem Header="Bass">
                <StackPanel Margin="0, 10, 0 ,0" helper:NotifierPanelHelper.EnableNotifier="True">
                    <TextBlock HorizontalAlignment="Center">Email</TextBlock>
                    <TextBox Width="150" Text="{Binding BassUser}"></TextBox>
                    <TextBlock HorizontalAlignment="Center">Code</TextBlock>
                    <TextBox Width="150" Text="{Binding BassCode}"></TextBox>

                    <TextBlock Margin="0, 10, 0, 5" HorizontalAlignment="Center">
                        <Hyperlink NavigateUri="{StaticResource basshomepage}" 
                                   Command="{x:Static view:Commands.OpenUrlCommand}"
                                   CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=NavigateUri}">
                            <Image Source="/img/logo_bass.gif" Stretch="None" />
                        </Hyperlink>                        
                    </TextBlock>

                    <Label FontSize="10" HorizontalAlignment="Center">
                        <Hyperlink NavigateUri="{StaticResource basshomepage}" 
                                   Command="{x:Static view:Commands.OpenUrlCommand}"
                                   CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=NavigateUri}">
                            Register
                        </Hyperlink>
                    </Label>
                    <TextBlock FontSize="11" Margin="0, 5, 0, 5" HorizontalAlignment="Center">
                        * Registration-Key remove splash screen
                    </TextBlock>
                </StackPanel>
            </TabItem>
            <TabItem Header="About">
                <Border Width="Auto" Margin="5, 10, 5, 5" HorizontalAlignment="Stretch" >
                    <Grid Margin="10, 10, 10, 10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>

                        <Label Content="{StaticResource appTitle}" FontSize="10"></Label>
                        <Label Grid.Column="1" FontSize="10" >
                            <Hyperlink NavigateUri="{StaticResource apphomepage}" 
                                   Command="{x:Static view:Commands.OpenUrlCommand}"
                                   CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=NavigateUri}">
                                Site
                            </Hyperlink>
                        </Label>

                        <Label Content="Version" Grid.Row="1" FontSize="10"></Label>
                        <Label Content="{Binding Source={StaticResource version}}" Grid.Column="1" Grid.Row="1" FontSize="10"></Label>

                        <Label Grid.Row="2" Content="© Fabrizio Stellato" FontSize="10"></Label>
                        <Label FontSize="10" Grid.Row="2" Grid.Column="1">
                            <Hyperlink NavigateUri="{StaticResource devhomepage}"
                                   Command="{x:Static view:Commands.OpenUrlCommand}"
                                   CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=NavigateUri}">
                                Site
                            </Hyperlink>
                        </Label>


                    </Grid>

                </Border>
            </TabItem>
        </TabControl>

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button IsEnabled="False" Content="Save" Command="{Binding Source={x:Static settings:SettingsManager.Instance},  Path=SavePreferencesCommand}" HorizontalAlignment="Center" Margin="5" Width="80" x:Name="savebtn" />
            <Button IsEnabled="False" Content="Reload" Command="{Binding Source={x:Static settings:SettingsManager.Instance},  Path=LoadPreferencesCommand}" HorizontalAlignment="Center" Margin="5" Width="80" x:Name="reloadbtn" />
            <!-- just a trick to close window with escape button -->
            <Button IsCancel="True" HorizontalAlignment="Center" Width="0" />
        </StackPanel>

    </StackPanel>

    <i:Interaction.Triggers>
        <!-- changed elements in notifier panel event -->
        <helper:RoutedEventTrigger RoutedEvent="{x:Static helper:NotifierPanelHelper.ChildrenChangedEvent}">
            <ei:ChangePropertyAction TargetName="savebtn" PropertyName="IsEnabled" Value="True" />
            <ei:ChangePropertyAction TargetName="reloadbtn" PropertyName="IsEnabled" Value="True" />
        </helper:RoutedEventTrigger>

        <!-- check updates event -->
        <i:EventTrigger EventName="SoftwareOutOfDateEvent" SourceObject="{Binding}">
            <i:Interaction.Behaviors>
                <helper:EventTriggerBehavior Attacher="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window} }}" />
            </i:Interaction.Behaviors>
            <view:InvokeDelegateCommandAction 
                Command="{x:Static view:Commands.ShowMessageOutOfDateVersionCommand}" 
                CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=InvokeParameter}" 
                />
        </i:EventTrigger>
        <i:EventTrigger EventName="SoftwareUpToDateEvent" SourceObject="{Binding}">            
            <i:Interaction.Behaviors>
                <helper:EventTriggerBehavior Attacher="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window} }}" />
            </i:Interaction.Behaviors>
            <helper:ShowMessageBoxAction Message="{StaticResource softwareUpToDateMessage}" Title="Info" />
        </i:EventTrigger>
        <i:EventTrigger EventName="NetworkErrorEvent" SourceObject="{Binding}">
            <i:Interaction.Behaviors>
                <helper:EventTriggerBehavior Attacher="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window} }}" />
            </i:Interaction.Behaviors>
            <helper:ShowMessageBoxAction Message="{StaticResource networkErrorMessage}" Title="Error" />
        </i:EventTrigger>

        <!-- settings event -->
        <i:EventTrigger EventName="SettingsSaved" SourceObject="{x:Static settings:SettingsManager.Instance}">
            <ei:ChangePropertyAction TargetName="savebtn" PropertyName="IsEnabled" Value="False" />
            <ei:ChangePropertyAction TargetName="reloadbtn" PropertyName="IsEnabled" Value="False" />
        </i:EventTrigger>
        <i:EventTrigger EventName="SettingsReloaded" SourceObject="{x:Static settings:SettingsManager.Instance}">
            <ei:ChangePropertyAction TargetName="savebtn" PropertyName="IsEnabled" Value="False" />
            <ei:ChangePropertyAction TargetName="reloadbtn" PropertyName="IsEnabled" Value="False" />
        </i:EventTrigger>
        
        <!-- Load event -->
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding InitCommand}" />            
        </i:EventTrigger>             
    </i:Interaction.Triggers>    

</Window>
